<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-form
        :inline="true"
        :model="queryParams"
        ref="resorceFormRef"
        class="demo-form-inline"
      >
        <el-form-item label="资源名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="资源名称"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label="资源路径" prop="url">
          <el-input
            v-model="queryParams.url"
            placeholder="资源路径"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label="资源分类" prop="categoryId">
          <el-select
            v-model="queryParams.categoryId"
            clearable
            placeholder="资源分类"
          >
            <el-option label="不限" value=""></el-option>
            <el-option
              v-for="item in categorys"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button @click="handleRest">重置</el-button>
          <el-button type="primary" @click="loadResource" :disabled="isLoading"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-button @click="handleDlg(0)">添加</el-button>
    <el-button @click="$router.push({ name: 'resource-category' })"
      >资源分类</el-button
    >
    <el-table
      :data="queryResult.records"
      border
      style="width: 100%"
      v-loading="isLoading"
    >
      <el-table-column type="index" width="80" align="center">
      </el-table-column>
      <el-table-column prop="name" label="资源名称" align="center">
      </el-table-column>
      <el-table-column prop="url" label="资源路径" align="center">
      </el-table-column>
      <el-table-column prop="description" label="描述" align="center">
      </el-table-column>
      <el-table-column
        prop="createdTime"
        label="添加时间"
        :formatter="timeFormater"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template v-slot="scoped">
          <el-button size="mini" @click="handleDlg(scoped.row.id)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scoped.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.current"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="queryResult.total"
      :disabled="isLoading"
    >
    </el-pagination>
    <resource-create-or-update
      ref="resourceDlg"
      @resource-change="loadResource"
    ></resource-create-or-update>
  </el-card>
</template>
<script>
import { queryResource, deleteResource } from '@/api/resource'
import { getAllResourceCategory } from '@/api/resource-category'
import dayjs from 'dayjs'
import ResourceCreateOrUpdate from './create-or-edit.vue'

export default {
  name: 'ResourceIndex',
  components: { ResourceCreateOrUpdate },
  data () {
    return {
      // 查询条件
      queryParams: {
        id: undefined,
        name: undefined,
        startCreateTime: undefined,
        url: undefined,
        categoryId: '',
        endCreateTime: undefined,
        current: 1,
        size: 10
      },
      // 查询结果
      queryResult: {},
      categorys: [],
      // 是否加载中
      isLoading: false
    }
  },
  created () {
    this.loadResource()
    this.loadCategory()
  },
  methods: {
    // 添加or编辑dialog
    handleDlg (id) {
      this.$refs.resourceDlg.showAndInit(id)
    },
    timeFormater (row, column, value, index) {
      return dayjs(value).format('YYYY-MM-DD')
    },
    // 获取资源
    loadResource () {
      this.isLoading = true
      queryResource(this.queryParams)
        .then((res) => {
          if (res.data.code === '000000') {
            const { current, pages, records, size, total } = res.data.data
            Object.assign(this.queryParams, { current, size })
            this.queryResult = { pages, records, total }
          } else {
            this.$message.error('获取资源数据失败')
          }
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    // 获取资源类别
    loadCategory () {
      getAllResourceCategory().then((res) => {
        if (res.data.code === '000000') {
          this.categorys = res.data.data
        } else {
          this.$message.error('获取资源类别失败')
        }
      })
    },
    // 重置表单
    handleRest () {
      this.$refs.resorceFormRef.resetFields()
    },
    handleSizeChange (val) {
      this.queryParams.size = val
      this.queryParams.current = 1
      this.loadResource()
    },
    handleCurrentChange (val) {
      this.queryParams.current = val
      this.loadResource()
    },
    // 删除
    handleDelete (id) {
      this.$confirm('确认要删除资源类别吗？', '删除提示')
        .then(() => {
          // 如果点确定
          deleteResource(id).then((res) => {
            if (res.data.code === '000000') {
              this.$message.success('删除资源成功')
              this.loadResource()
            } else {
              this.$message.error('删除资源失败')
            }
          })
        })
        .catch(() => {
          // 如果点取消
          this.$message.info('取消了删除资源类别的操作')
        })
    }
  }
}
</script>
<style scoped lang="scss">
.el-table {
  margin-top: 20px;
}
</style>
